#!/bin/bash
# 功能: 安装 Nginx
# 名称: deploy_install.sh
# 作者: zxm
# 创建日期: 2024-12-18
# 版本: v1.1

FIND_STR="UINO_HOME"
FIND_FILE_ETC_PROFILE=/etc/profile
FIND_FILE_BASHRC=~/.bashrc
FIND_FILE_BASH_PROFILE=~/.bash_profile

etc_profile_num=$(grep -c $FIND_STR $FIND_FILE_ETC_PROFILE)
bashrc_num=$(grep -c $FIND_STR $FIND_FILE_BASHRC)
bash_profile=$(grep -c $FIND_STR $FIND_FILE_BASH_PROFILE)

if [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -eq '0' ]] && [[ ${bashrc_num} -eq '0' ]] && [[ ${bash_profile} -eq '0' ]]; then
        echo "UINO_HOME is not exist,please input a directory to install(default:/uinnova)"
        read UINO_INPUT
        if [ ! ${UINO_INPUT} ]; then
                UINO_INPUT=/uinnova
        fi
        if [ ! -d ${UINO_INPUT} ]; then
                mkdir -p ${UINO_INPUT}
        fi
        if [ -w "${UINO_INPUT}" ] && [ -x "${UINO_INPUT}" ]; then
                echo "modify privileages ...."
        else
                echo "you have no permission with ${UINO_INPUT},please conntact administrator to add permission or use another directory!"
                exit 0
        fi
        echo "export UINO_HOME=${UINO_INPUT}" >>~/.bashrc
        source ~/.bashrc
elif [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -gt '0' ]] && [[ ${bashrc_num} -eq '0' ]] && [[ ${bash_profile} -eq '0' ]]; then
        source /etc/profile
elif [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -eq '0' ]] && [[ ${bashrc_num} -gt '0' ]] && [[ ${bash_profile} -eq '0' ]]; then
        source ~/.bashrc
elif [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -eq '0' ]] && [[ ${bashrc_num} -eq '0' ]] && [[ ${bash_profile} -eq '0' ]]; then
        source ~/.bash_profile
elif [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -gt '0' ]] && [[ ${bashrc_num} -gt '0' ]] && [[ ${bash_profile} -gt '0' ]]; then
        UINO_HOME exist in "/etc/profile" and "~/bashrc" and "~/bash_profile",please Re-install after clear two places
elif [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -gt '0' ]] && [[ ${bashrc_num} -gt '0' ]] && [[ ${bash_profile} -eq '0' ]]; then
        UINO_HOME exist in "/etc/profile" and "~/bashrc" ,please Re-install after clear one place
elif [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -gt '0' ]] && [[ ${bashrc_num} -eq '0' ]] && [[ ${bash_profile} -gt '0' ]]; then
        UINO_HOME exist in "/etc/profile" and "~/bash_profile",please Re-install after clear one place
elif [[ ! ${UINO_HOME} ]] && [[ ${etc_profile_num} -eq '0' ]] && [[ ${bashrc_num} -gt '0' ]] && [[ ${bash_profile} -gt '0' ]]; then
        UINO_HOME exist in "~/.bashrc" and "~/bash_profile",please Re-install after clear one place
else
        echo "UINO_HOME is ${UINO_HOME}"
fi

cd $(dirname $0)
curPath=$(pwd)

#检查gcc
data=$(g++ --version)
gccinfo=""

if [ "$data" = "$gccinfo" ]; then
        echo "g++ can not be found,please install g++ first!"
        exit 0
fi

#删除已存在的解压目录
find deploy/* -name '*' -type d | xargs rm -rf
path=${UINO_HOME}/nginx

if [ -d $path ]; then
        if [[ ! -x $path ]] || [[ ! -w $path ]]; then
                echo "user ${USERNAME} has no permission to write in this directory,please modify the value of the UINO_HOME in ~/.bashrc,and re_install again!"
                exit 0

        fi
else
        mkdir -p $path
        if [[ $? == 1 ]]; then
                echo "user ${USERNAME} has no permission to write in this directory,please modify the value of the UINO_HOME in ~/.bashrc,and re_install again!"
                exit 0
        fi
fi

echo "Start to install perl..."

sleep 3

#安装perl
sh scripts/perl_install.sh $path

#解压openssl
echo "decompress openssl..."
sleep 1
tar xf deploy/openssl-1.1.1b.tar.gz -C deploy

#解压pcre
echo "decompress pcre..."
sleep 1
#sh scripts/pcre_install.sh $path
tar xf deploy/pcre-8.43.tar.gz -C deploy

#解压zlib
echo "decompress zlib..."
sleep 1
#sh scripts/zlib_install.sh $path
tar xf deploy/zlib-1.2.11.tar.gz -C deploy

#安装yasm ffmpeg依赖

echo "start to install nginx with yasm ffmpeg"
#sleep 3
sh scripts/dcv_nginx_install.sh $path

#cp deploy/nginx-rtmp-module-master.tar.gz ./
tar xf deploy/nginx-rtmp-module-master.tar.gz -C deploy

#if [ -d perl-5.24.1 ];then
# rm -rf perl-5.24.1
#fi

#解压nginx
tar -xzf deploy/nginx-1.26.2.tar.gz -C deploy
cd deploy/nginx-1.26.2

./configure --prefix=${path}/nginx --add-module=../nginx-rtmp-module-master --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=../openssl-1.1.1b --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.43 --with-http_v2_module

make && make install

cd ${curPath}
# rm -rf deploy/openssl-1.1.1b
# rm -rf deploy/pcre-8.43
# rm -rf deploy/zlib-1.2.11
# rm -rf deploy/nginx-rtmp-module-master
# rm -rf deploy/nginx-nginx-1.26.2
